package com.yanqu.road.dao.impl.activity.newyearbag;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NewYearBagUserDataDaoImpl extends TempDataObjectDao<NewYearBagUserData> {

    public static String getNewYearBagUserDataSql = "select * from `t_c_new_year_bag_user_data` where `activityId`=? ";

    public Map<Long, NewYearBagUserData> getNewYearBagUserDataMap(int activityId) {
        Map<Long, NewYearBagUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewYearBagUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewYearBagUserData parse = parse(rs);
                map.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateNewYearBagUserDataSql = "update `t_c_new_year_bag_user_data` set allReward=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(NewYearBagUserData newYearBagUserData) {
        newYearBagUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, newYearBagUserData.getAllReward()));
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, newYearBagUserData.getUserId()));
        if (getDb().executeNoneQuery(updateNewYearBagUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewYearBagUserDataSql = "insert into `t_c_new_year_bag_user_data`(activityId,userId,allReward) values (?,?,?)";

    @Override
    protected boolean addData(NewYearBagUserData newYearBagUserData) {
        newYearBagUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, newYearBagUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, newYearBagUserData.getAllReward()));
        if (getDb().executeNoneQuery(insertNewYearBagUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected NewYearBagUserData parseData(ResultSet rs) throws SQLException {
        NewYearBagUserData newYearBagUserData = new NewYearBagUserData();
        newYearBagUserData.setActivityId(rs.getInt("activityId"));
        newYearBagUserData.setUserId(rs.getLong("userId"));
        newYearBagUserData.setAllReward(rs.getString("allReward"));
        return newYearBagUserData;
    }
}
